SELECT  formmain_0235.ID,A.ID RequesterName,B.ID PreparerName,C.ID InvoiceApproverName,
formmain_0233.field0001 as PONumber,formmain_0233.field0003 as POName,
formmain_0233.field0004 as POStatus,formmain_0233.field0005 as POTotalAmount,formmain_0233.field0006 as BusinessUnit,
formmain_0233.field0009 as ReqCreateDate,
formmain_0233.field0010 as POCreateDate,formmain_0233.field0011 as POLastModifiedDate,formmain_0233.field0012 as RequisitionNumber,
formmain_0233.field0013 as PreparerWWID,formmain_0233.field0014 as PreparerName1,formmain_0233.field0015 as RequesterWWID,
formmain_0233.field0016 as RequesterName1,max(formmain_0233.field0018) as InvoiceApproverWWID,
formmain_0233.field0019 as InvoiceApproverName1,formmain_0233.field0021 as SupplierID,
formmain_0233.field0022 as SupplierName,formmain_0233.field0025 as Currency,formmain_0233.field0026 as PreprNotReqstrInd,
formmain_0233.field0028 as POCloseIndicator,max(formmain_0233.field0029) as POCloseDate,formmain_0233.field0020 as PACTSuplrNumber,
max(formmain_0234.field0014) as InternalOrderNumber
FROM formmain_0233
LEFT JOIN formmain_0234 ON formmain_0234.field0001 = formmain_0233.field0001
LEFT JOIN formmain_0235 ON formmain_0235.field0001=formmain_0233.field0001
LEFT JOIN ORG_MEMBER A ON formmain_0233.field0015=A.CODE 
LEFT JOIN ORG_MEMBER B ON formmain_0233.field0013=B.CODE
LEFT JOIN ORG_MEMBER C ON formmain_0233.field0018=C.CODE
where (A.id IN (
 SELECT id
 FROM  org_member
 WHERE  IS_ENABLE = 1 and IS_DELETED = 0  and IS_ADMIN = 0
 UNION ALL
 SELECT  id
 FROM(
  SELECT  max( id)id,   code,name  FROM   org_member
  WHERE  (IS_ENABLE = 0  OR IS_DELETED = 1) group by code ,name
  ) deletes
  WHERE NOT EXISTS (  SELECT code   FROM (
      SELECT code
      FROM org_member
      WHERE     IS_ENABLE = 1    and IS_DELETED = 0   and IS_ADMIN = 0 
     ) enables
    WHERE
     deletes.code = enables.code
   )
)or A.ID is null)
AND (B.id IN  (
 SELECT id
 FROM  org_member
 WHERE  IS_ENABLE = 1 and IS_DELETED = 0  and IS_ADMIN = 0
 UNION ALL
 SELECT  id
 FROM(
  SELECT  max( id)id,   code,name  FROM   org_member
  WHERE  (IS_ENABLE = 0  OR IS_DELETED = 1) group by code ,name
  ) deletes
  WHERE NOT EXISTS (  SELECT code   FROM (
      SELECT code
      FROM org_member
      WHERE     IS_ENABLE = 1    and IS_DELETED = 0   and IS_ADMIN = 0 
     ) enables
    WHERE
     deletes.code = enables.code
   )
)or B.id is null)
AND( c.id IN   (
 SELECT id
 FROM  org_member
 WHERE  IS_ENABLE = 1 and IS_DELETED = 0  and IS_ADMIN = 0
 UNION ALL
 SELECT  id
 FROM(
  SELECT  max( id)id,   code,name  FROM   org_member
  WHERE  (IS_ENABLE = 0  OR IS_DELETED = 1) group by code ,name
  ) deletes
  WHERE NOT EXISTS (  SELECT code   FROM (
      SELECT code
      FROM org_member
      WHERE     IS_ENABLE = 1    and IS_DELETED = 0   and IS_ADMIN = 0 
     ) enables
    WHERE
     deletes.code = enables.code
   )
)or c.id is null)
AND formmain_0235.ID is null
AND (formmain_0233.field0021='5250024557'
OR formmain_0233.field0021='5250029106'
OR formmain_0233.field0021='5250046396' 
OR formmain_0233.field0021='5250050506' 
OR formmain_0233.field0021='5250063626'
OR formmain_0233.field0021='5250109961'
OR formmain_0233.field0021='5250110215'
OR formmain_0233.field0021='5250123458')
GROUP BY formmain_0233.field0001,
formmain_0233.field0003,
formmain_0233.field0004 ,formmain_0233.field0005,formmain_0233.field0006,
formmain_0233.field0009,
formmain_0233.field0010 ,formmain_0233.field0011,formmain_0233.field0012 ,
formmain_0233.field0013 ,formmain_0233.field0014 ,formmain_0233.field0015 ,
formmain_0233.field0016 ,
formmain_0233.field0019 ,formmain_0233.field0021,
formmain_0233.field0022 ,formmain_0233.field0025,formmain_0233.field0026,
formmain_0233.field0028,formmain_0233.field0020,
formmain_0235.ID,A.ID,B.ID,C.ID
ORDER BY PONumber desc
